// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   License.java

package com.jivesoftware.forum;

import com.jivesoftware.jdom.Document;
import com.jivesoftware.jdom.Element;
import com.jivesoftware.jdom.input.SAXBuilder;
import com.jivesoftware.jdom.output.XMLOutputter;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class License
{
    /* member class not found */
    class LicenseType {}


    private long licenseID;
    private String product;
    private String version;
    private LicenseType licenseType;
    private String name;
    private String company;
    private int numCopies;
    private Date expiresDate;
    private String signature;
    private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");

    License(long l, String s, String s1, LicenseType licensetype)
    {
        licenseID = l;
        product = s;
        version = s1;
        licenseType = licensetype;
        name = null;
        company = null;
        s = null;
        numCopies = 1;
        expiresDate = null;
        signature = null;
    }

    public long getLicenseID()
    {
        return licenseID;
    }

    public void setLicenseID(long l)
    {
        licenseID = l;
    }

    public String getProduct()
    {
        return product;
    }

    public void setProduct(String s)
    {
        product = s;
    }

    public String getVersion()
    {
        return version;
    }

    public void setVersion(String s)
    {
        version = s;
    }

    public LicenseType getLicenseType()
    {
        return licenseType;
    }

    public void setLicenseType(LicenseType licensetype)
    {
        licenseType = licensetype;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String s)
    {
        name = s;
    }

    public String getCompany()
    {
        return company;
    }

    public void setCompany(String s)
    {
        company = s;
    }

    public int getNumCopies()
    {
        return numCopies;
    }

    public void setNumCopies(int i)
    {
        numCopies = i;
    }

    public Date getExpiresDate()
    {
        if(expiresDate == null)
            return null;
        else
            return new Date(expiresDate.getTime());
    }

    public void setExpiresDate(Date date)
    {
        expiresDate = date;
    }

    public String getSignature()
    {
        return signature;
    }

    public void setSignature(String s)
    {
        signature = s;
    }

    public byte[] getFingerprint()
    {
        StringBuffer stringbuffer = new StringBuffer(100);
        stringbuffer.append(licenseID);
        stringbuffer.append(product);
        stringbuffer.append(version);
        stringbuffer.append(licenseType);
        stringbuffer.append(numCopies);
        if(expiresDate != null)
            stringbuffer.append(formatter.format(expiresDate));
        if(name != null)
            stringbuffer.append(name);
        if(company != null)
            stringbuffer.append(company);
        return stringbuffer.toString().getBytes();
    }

    public boolean equals(Object obj)
    {
        if(!(obj instanceof License))
            return false;
        else
            return this == obj || licenseID == ((License)obj).getLicenseID();
    }

    public static String toXML(License license)
        throws Exception
    {
        Element element = new Element("license");
        element.addContent((new Element("licenseID")).addContent("" + license.getLicenseID()));
        element.addContent((new Element("product")).addContent(license.getProduct()));
        element.addContent((new Element("licenseType")).addContent(license.getLicenseType().toString()));
        element.addContent((new Element("name")).addContent(license.getName() != null ? license.getName() : ""));
        element.addContent((new Element("company")).addContent(license.getCompany() != null ? license.getCompany() : ""));
        element.addContent((new Element("version")).addContent(license.getVersion()));
        element.addContent((new Element("numCopies")).addContent("" + license.getNumCopies()));
        element.addContent((new Element("expiresDate")).addContent(license.getExpiresDate() != null ? formatter.format(license.getExpiresDate()) : ""));
        element.addContent((new Element("signature")).addContent(license.getSignature() != null ? license.getSignature() : ""));
        Document document = new Document(element);
        XMLOutputter xmloutputter = new XMLOutputter();
        StringWriter stringwriter = new StringWriter(500);
        xmloutputter.output(document, stringwriter);
        return stringwriter.toString();
    }

    public static License fromXML(String s)
        throws Exception
    {
        SAXBuilder saxbuilder = new SAXBuilder();
        StringReader stringreader = new StringReader(s);
        Document document = saxbuilder.build(stringreader);
        stringreader.close();
        Element element = document.getRootElement();
        int i = Integer.parseInt(element.getChild("licenseID").getText());
        String s1 = element.getChild("product").getText();
        String s2 = element.getChild("version").getText();
        LicenseType licensetype = LicenseType.fromString(element.getChild("licenseType").getText());
        License license = new License(i, s1, s2, licensetype);
        String s3 = element.getChild("name").getText();
        if(s3 != null && !s3.equals(""))
            license.setName(s3);
        String s4 = element.getChild("company").getText();
        if(s4 != null && !s4.equals(""))
            license.setCompany(s4);
        license.setNumCopies(Integer.parseInt(element.getChild("numCopies").getText()));
        String s5 = element.getChild("expiresDate").getText();
        if(s5 != null && !s5.equals(""))
        {
            Date date = formatter.parse(s5);
            license.setExpiresDate(date);
        }
        license.setSignature(element.getChild("signature").getText());
        return license;
    }

}
